<?php
namespace app\model;


/**
 * Author: cbw
 * Desc:对接内置微信服务商模式下的小程序
 * Time: 2023/6/21 17:01
 * Email: 17854288794@163.com
 */
require_once PATH_MODULE .'define.php';
class ThirdWxService
{
    private static $webDomain_url = 'https://oa.yixuan.net/yx99999/2/0/0/index.aspx?do=wxservice&op=WebViewDomain';
    private static $releaseWxApp_url = 'https://oa.yixuan.net/yx99999/2/0/0/index.aspx?do=wxservice&op=Release';
    private static $submitAudit_url = 'https://oa.yixuan.net/yx99999/2/0/0/index.aspx?do=wxservice&op=SubmitAudit';
    private static $getTemplateList_url = 'https://oa.yixuan.net/yx99999/2/0/0/index.aspx?do=wxservice&op=GetTemplatelist';
    private static $revertCodeRelease_url = 'https://oa.yixuan.net/yx99999/2/0/0/index.aspx?do=wxservice&op=Revertcoderelease';
    private static $latestAudioStatus_url = 'https://oa.yixuan.net/yx99999/2/0/0/index.aspx?do=wxservice&op=LatestAuditStatus';
    private static $jumpDomain_url = 'https://oa.yixuan.net/yx99999/2/0/0/index.aspx?do=wxservice&op=JumpDomain';

    /**
     * Desc:修改业务域名
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2023/6/21 17:09
     * @param $authorizer_appid [授权第三方appid
     * @param $webviewdomain [业务域名
     * @return mixed
     */
    public static function modifyWebDomain($authorizer_appid,$webviewdomain)
    {
        $post_data = ['authorizer_appid'=>$authorizer_appid,'webviewdomain'=>$webviewdomain];
        $res = request(self::$webDomain_url,json_encode($post_data));
        if ($res['error']!=1){
            return ['code'=>0,'msg'=>$res['msg'],'data'=>[]];
        }else{
            return ['code'=>1,'msg'=>$res['msg'],'data'=>$res['data']];
        }
    }

    /**
     * Desc:发布小程序
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2023/6/21 17:14
     * @param $authorizer_appid
     * @return mixed
     */
    public static function releaseWxApp($authorizer_appid)
    {
        $post_data = ['authorizer_appid'=>$authorizer_appid];
        $res =  request(self::$releaseWxApp_url,$post_data);
        if ($res['error']!=1){
            return ['code'=>0,'msg'=>$res['msg'],'data'=>[]];
        }else{
            return ['code'=>1,'msg'=>$res['msg'],'data'=>$res['data']];
        }
    }

    /**
     * Desc:上传并提审
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2023/6/21 17:45
     */
    public static function submitAudit($authorizer_appid,$ext_json,$template_id,$user_version,$user_desc)
    {
        $post_data = ['authorizer_appid'=>$authorizer_appid,'ext_json'=>json_encode($ext_json),'template_id'=>$template_id,'user_version'=>$user_version,'user_desc'=>$user_desc];
        $res = request(self::$submitAudit_url,json_encode($post_data));
        if ($res['error']!=1){
            return ['code'=>0,'msg'=>$res['msg'],'data'=>[]];
        }else{
            return ['code'=>1,'msg'=>$res['msg'],'data'=>$res['data']];
        }
    }

    /**
     * Desc:获取小程序可发版本列表数据
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2023/6/21 17:27
     * @return array
     */
    public static function getTemplateList()
    {
        $res = request(self::$getTemplateList_url);
        if ($res['error']!=1){
            return ['code'=>0,'msg'=>$res['errmsg'],'data'=>[]];
        }else{
            $data = [];
            $default = $res['data']['default'];
            $list = $res['data']['list'];
            foreach ($list as $k => $v){
                $tmp_arr = ['template_id'=>$v['template_id'],'user_version'=>$v['user_version'],'user_desc'=>$v['user_desc']];
                array_push($data,$tmp_arr);
            }
            return ['default'=>$default,'list'=>$data];
        }
    }

    /**
     * Desc:版本回退
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2023/6/22 09:47
     * @param $authorizer_appid
     * @return array
     */
    public static function revertCodeRelease($authorizer_appid)
    {
        $res = request(self::$revertCodeRelease_url.'&authorizer_appid='.$authorizer_appid);
        if ($res['error']!=1){
            return ['code'=>0,'msg'=>$res['msg'],'data'=>[]];
        }else{
            return ['code'=>1,'msg'=>$res['msg'],'data'=>$res['data']];
        }
    }

    /**
     * Desc:查询最新审核
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2023/6/22 10:33
     * @param $authorizer_appid
     * @return array
     */
    public static function latestAudioStatus($authorizer_appid)
    {
        $get_url = self::$latestAudioStatus_url.'&authorizer_appid='.$authorizer_appid;
        $res = request($get_url);
        if ($res['error']!=1){
            return ['code'=>0,'msg'=>$res['msg'],'data'=>[]];
        }else{
            return ['code'=>1,'msg'=>$res['msg'],'data'=>$res['data']];
        }
    }

    /**
     * Desc:获取服务商业务域名
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2023/6/23 10:22
     * @return array
     */
    public static function getWebDomain()
    {
        $post_data = ['type'=>'get'];
        $res = request(self::$jumpDomain_url,json_encode($post_data));
        if ($res['data'] && $res['data']['published_wxa_jump_h5_domain']){
            return ['code'=>1,'msg'=>$res['msg'],'data'=>$res['data']['published_wxa_jump_h5_domain']];
        }else{
            return ['code'=>0,'msg'=>$res['msg'],'data'=>[]];
        }
    }

    /**
     * Desc:设置服务商业务域名
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2023/6/23 10:30
     * @param $domain
     * @return array
     */
    public static function setWebDomain($domain)
    {
        $post_data = ['type'=>'set','domain'=>$domain];
        $res = request(self::$jumpDomain_url,json_encode($post_data));
        if ($res['error']!=1){
            return ['code'=>0,'msg'=>$res['msg'],'data'=>[]];
        }else{
            return ['code'=>1,'msg'=>$res['msg'],'data'=>$res['data']['published_wxa_jump_h5_domain']];
        }
    }

    /**
     * Desc:格式化上传参数转换为ext.json格式
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2023/6/21 18:34
     * @param $str
     */
    public static function formatScene($appid,$uniacid,$scene,$user_version)
    {
        $tmp_appid =  $appid;
        $siteroot = 'https://17.yixuan.net';
        $siteroot2 = 'https://17.yixuansoft.cn';
        $siteroot3 = 'https://17.kissbaby.net.cn';
        $siteroot4 = 'https://17.xgbb.net';
        $siteroot5 = 'https://17.yixuan.net';
        if (strpos($scene,'18')!==false){
            $siteroot = 'https://18.yixuan.net';
            $siteroot2 = 'https://18.yixuansoft.cn';
            $siteroot3 = 'https://18.kissbaby.net.cn';
            $siteroot4 = 'https://18.xgbb.net';
            $siteroot5 = 'https://18.yixuan.net';
        }
//        测试域名 合并代码删除
//        $siteroot = 'https://5.yixuan.net';
//        $siteroot2 = 'https://5.yixuansoft.cn';
//        $siteroot3 = 'https://5.kissbaby.net.cn';
//        $siteroot4 = 'https://5.xgbb.net';
//        $siteroot5 = 'https://5.yixuan.net';

        $ext_version = 'W'.$user_version;
        $default_info = [
            'uniacid'=>$uniacid,
            'scene'=>$scene,
            'siteroot'=>$siteroot,
            'siteroot2'=>$siteroot2,
            'siteroot3'=>$siteroot3,
            'siteroot4'=>$siteroot4,
            'siteroot5'=>$siteroot5,
        ];
        $format_ext_arr = [
            'version'=>$ext_version,
            'default_appID'=>$tmp_appid,
            'default_info'=>$default_info
        ];
        $formt_ext_json_arr = [
            'extEnable'=>true,
            'extAppid'=>$tmp_appid,
            'directCommit'=>false,
            'ext'=>$format_ext_arr
        ];
        return $formt_ext_json_arr;
    }

    /**
     * Desc:创建sql
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2023/6/22 10:30
     */
    public static function installSql()
    {
        $sql = "
                CREATE TABLE IF NOT EXISTS `yx_third_wxapp` (
                    `id` int(11) not null AUTO_INCREMENT,
                    `uniacid` int(11) not null default 0 comment 'uniacid',
                    `appid` varchar(32) not null default '' comment '小程序appid',
                    `online_version` varchar(12) not null default '' comment '线上版本',
                    `template_id` varchar(12) not null default '' comment '线上版本模板id',
                    `status` int(1)  not null default -1 comment '状态 -1未提审 0审核成功，1被拒绝，2审核中，3已撤回，4审核延后，5已发布',
                    `status_desc` text   comment '状态说明 ',
                    `ext_json` text   comment 'ext内容',
                    `op_log` text   comment '备注',
                    `admin_name` varchar(64) not null default '' comment '操作人',
                    `ip` varchar(128) not null default '' comment '操作人ip',
                    `created_at` timestamp default CURRENT_TIMESTAMP comment '创建时间',
                    `updated_at` timestamp default CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP comment '更新时间',
                    PRIMARY KEY (`id`)
                )ENGINE = InnoDB DEFAULT CHARACTER SET =utf8 COMMENT='服务商对接小程序记录表';
        ";
        db_query($sql);
        yxlog('创建第三方对接小程序记录表');
    }

    /**
     * Desc:微信服务商上传小程序初始化
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2023/6/22 14:01
     * @param $uniacid
     * @param $appid
     */
    public static function init($uniacid,$appid)
    {
        global $_G;
        if (!db_tableexists(third_wxapp)){
            self::installSql();
        }
        //判断当前小程序是否存在记录
        $res = db_get(third_wxapp,['uniacid'=>$uniacid,'appid'=>$appid]);
        if (!$res){
            $add_res = $insertid = db_insert(third_wxapp,['uniacid'=>$uniacid,'appid'=>$appid,'ip'=>$_G['clientip'],'admin_name'=>(string)$_G['username']]);
        }
    }
}